var get=require('../../utils/getRequest');
var post=require('../../utils/postRequest');
const api= require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      agentFlag:0,
      active:0,
      ball01Log:"/images/4G.png",
      ball02Log:"/images/blueteeth.png",
      xwDeviceList:null,
      b4G:0,
      bluetooth:0,
      allCount:0,
      xwWashballList:null,
      buyOrderList:[],
      rentOrderList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({title:'努力加载中'})
    console.log("openId:"+api.openId)
    var that=this;
    if(api.openId==null){
        wx.login({
          success: (res) => {
            var code=res.code;
            var data={code:code};
            get.getRequest(null,api.getOpenIdByCode,data).then(res=>{
              console.log("res.code:",res.code)
              console.log("res.data.openId:",res.data.openId)
              if (res.code == 200) {
                api.openId=res.data.openId;
                that.getAgent(api.openId);
                that.initAgentOrder(api.openId);
             }
            })
          },
        })
    }else{
      that.getAgent(api.openId);
      that.initAgentOrder(api.openId);
    }
  },
getAgent(openId){
    var that=this;
    var data={openId:openId}
    get.getRequest(null,api.getAgentMessage,data).then(res=>{
      if (res.code == 200) {
        var bindState=res.data==null?res.data:res.data.bindState;
        var freezeState=res.data==null?res.data:res.data.freezeState;
        if(res.data==null){
          that.setData({
            agentFlag:1
          });
        }else if(bindState!=null&&bindState=="N"){
          wx.showModal({
            title: '提示信息',
            showCancel:false,
            content: "当前微信有代理商申请审核中，请耐心等待",
            success (res) {
              if (res.confirm) {
                wx.navigateBack({});
              } 
            }
          })
        }else if(freezeState!=null&&freezeState=="Y"){
          wx.showModal({
            title: '提示信息',
            showCancel:false,
            content: "当前账号已经被冻结",
            success (res) {
              if (res.confirm) {
                wx.navigateBack({});
              } 
            }
          })
        }else if(freezeState!=null){
          that.setData({
            agentFlag:2
          });
          that.getDeviceList();
        }
      }
    }).finally(res=>{
      wx.hideLoading();
    });
},
initAgentOrder(){
  var that=this;
  var data={openId:api.openId}
  get.getRequest(null,api.getAgentOrders,data).then(res=>{
    console.log(res);
        var buyOrderList=[];
        var rentOrderList=[];
        var orders=res.data;
        for(var i=0;i<orders.length;i++){
          if(orders[i].buyType=="1"&&orders[i].payState=="3"){
            rentOrderList.push(orders[i]);
          }
          else if(orders[i].buyType=="2"&&orders[i].payState=="3"){
            buyOrderList.push(orders[i]);
          }
        }
        that.setData({
          buyOrderList:buyOrderList,
          rentOrderList:rentOrderList
        })
  })
},
goToBingPage(){
  wx.navigateTo({
    url: '../../pages/agent/bingpage/bingpage',
  })
},
onTabsChange(e){
  console.log(e);
  var that=this;
  var index=e.detail.index;
  if(index==0){
    that.getDeviceList();
  }

},
getDeviceList(){
    var that=this;
    var data={openId:api.openId}
    wx.showLoading({title:'努力加载中'})
    get.getRequest(null,api.getAgentDeviceList,data).then(res=>{
       that.setData({
          b4G:res.data.b4G,
          bluetooth:res.data.bluetooth,
          allCount:res.data.allCount,
          xwDeviceList:res.data.xwDeviceList
        });
    }).finally(res=>{
      wx.hideLoading();
    });
},
unBingDevice(e){
  console.log(e);
  var that=this;
  var deviceSn=e.currentTarget.dataset.devicesn;
  var data={
    deviceSn:deviceSn,
    type:"1",
    openId:api.openId
  }
  wx.showModal({
    title: '提示信息',
    showCancel:true,
    content: "确定解绑设备："+deviceSn+"?",
    success (res) {
      if (res.confirm) {
            get.getRequest(null,api.unBingDeviceState,data).then(res=>{
              if(res.code==200){
                that.getDeviceList();
              }
              wx.showLoading({
                title: res.msg,
              })
            })
      } 
    }
  })
},
scanCode(e){
  console.log(e);
  var index=e.currentTarget.dataset.index;
  var that=this;
  wx.scanCode({
    onlyFromCamera:true,
    success:function(res){
      ///pages/index/index?deviceSn=13889878
      console.log(res);
      var path=res.path;
      try{
        var deviceSn=path.split("?")[1].split("=")[1];
        var type=deviceSn.substr(0,1);
        if(type!=1&&type!=2){
          wx.showToast({title:'不是合法的设备二维码'})
          return;
        }
        if(deviceSn.length!=8){
          wx.showToast({title:'不是合法的设备二维码'})
          return;
        }
        if(index=="1"){
            that.saveAgentDevice(deviceSn);
        }else{
             wx.navigateTo({
               url: '../../pages/agent/storagebluetooth/storagebluetooth?deviceSn='+deviceSn,
             })
        }
      }catch(e){
        wx.showToast({title:'不是合法的设备二维码'})
      }}
    })
},
saveAgentDevice(deviceSn){
  var that=this;
  var data={
    openId:api.openId,
    deviceSn:""+deviceSn,
    type:"1"
  }
  console.log("data:",data);
  wx.showLoading({title:'努力加载中'});
  get.getRequest(null,api.saveDevice,data).then(res=>{
      if(res.code==200){
        wx.showModal({
          title: '提示信息',
          showCancel:false,
          content: res.msg,
          success (res) {
            if (res.confirm) {
              that.getDeviceList();
            } 
          }
        })
      }else{
        wx.showModal({
          title: '提示信息',
          showCancel:false,
          content: res.msg,
          success (res) {
            
          }
        })
      }


  }).finally(res=>{
    wx.hideLoading();
  })
},
cashOut(){
  wx.navigateTo({
    url: '/pages/agent/cashout/cashout'
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})